package com.jx.mst.mst.controller;

import com.jx.mst.model.common.dtos.PageParams;
import com.jx.mst.model.common.dtos.ResponseResult;
import com.jx.mst.model.mst.dtos.MstInfoDtos;
import com.jx.mst.model.mst.dtos.MstInfoPageDtos;
import com.jx.mst.mst.entity.MstInfo;
import com.jx.mst.mst.entity.MstSel;
import com.jx.mst.mst.entity.dtos.MstInfoByIdDtos;
import com.jx.mst.mst.service.MstDetailService;
import com.jx.mst.mst.service.MstInfoService;
import jakarta.annotation.Resource;
//import org.springframework.data.domain.Page;
//import org.springframework.data.domain.PageRequest;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.jx.mst.model.common.redisCon.MseRedisCon.REDIS_PREX_MSTSEL;
import static com.jx.mst.model.common.redisCon.MseRedisCon.REDIS_ZSET_MSTINFO;

//import javax.annotation.Resource;

/**
 * (MstInfo)表控制层
 *
 * @author makejava
 * @since 2023-12-26 14:02:56
 */
@RestController
@RequestMapping("mstInfo")
public class MstInfoController {
    /**
     * 服务对象
     */
    @Resource
    private MstInfoService mstInfoService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Resource
    private MstDetailService mstDetailService;

    @PostMapping("/addMstInfo")
    public ResponseResult addMstInfo(HttpServletRequest request,@RequestBody(required = true) MstInfoDtos mstInfoDtos){
//        List<MstSel> list = mstSelService.query().orderByAsc("mst_sel_sort").list();

        return mstInfoService.addMstInfo(request,mstInfoDtos);
    }

    @PostMapping("/getMstInfo")
    public ResponseResult getMstInfo(@RequestBody(required = true) MstInfoPageDtos mstInfoPageDtos){
//        List<MstSel> list = mstSelService.query().orderByAsc("mst_sel_sort").list();
        return mstInfoService.queryByPage(mstInfoPageDtos);
    }

    @DeleteMapping("/delMstInfo")
    public ResponseResult delMstInfo(Long[] ids ) {
//        mstSel.setMstSelCreatetime(new Date());
        List<Long> collect = Arrays.stream(ids).collect(Collectors.toList());
        collect.forEach(item -> {
            stringRedisTemplate.opsForZSet().remove(REDIS_ZSET_MSTINFO,item.toString());

            MstInfo mstInfo = mstInfoService.getById(item);
            String count = stringRedisTemplate.opsForValue().get(REDIS_PREX_MSTSEL + mstInfo.getMstSelId());
            int counts = Integer.valueOf(count) -1;
            stringRedisTemplate.opsForValue().set(REDIS_PREX_MSTSEL + mstInfo.getMstSelId(),String.valueOf(counts));
        });
        mstDetailService.removeByIds(collect);
        boolean del = mstInfoService.removeByIds(collect);
        return ResponseResult.okResult("删除成功");
    }

    @GetMapping("/getMstInfoById")
    public ResponseResult getMstInfoById(@RequestParam(required = true) Long id){
//        List<MstSel> list = mstSelService.query().orderByAsc("mst_sel_sort").list();
        MstInfo byId = mstInfoService.getById(id);
        return ResponseResult.okResult(byId);
    }

    @PostMapping("/editMstInfo")
    public ResponseResult editMstInfo(@RequestBody(required = true) MstInfo mstInfo) {

        MstInfo byId = mstInfoService.getById(mstInfo.getId());

        String count = stringRedisTemplate.opsForValue().get(REDIS_PREX_MSTSEL + byId.getMstSelId());
        int counts = Integer.valueOf(count) -1;
        stringRedisTemplate.opsForValue().set(REDIS_PREX_MSTSEL + byId.getMstSelId(),String.valueOf(counts));

        String count2 = stringRedisTemplate.opsForValue().get(REDIS_PREX_MSTSEL + mstInfo.getMstSelId());
        int counts2 = Integer.valueOf(count2) +1;
        stringRedisTemplate.opsForValue().set(REDIS_PREX_MSTSEL + mstInfo.getMstSelId(),String.valueOf(counts2));

        boolean update = mstInfoService.updateById(mstInfo);
        return ResponseResult.okResult("修改成功");
    }

    @GetMapping("/getMstinfoById")
    public ResponseResult getMstinfoById(@RequestParam(value = "mstinfoId")String mstinfoId){
        MstInfoByIdDtos mstinfoById = mstInfoService.getMstinfoById(mstinfoId);
        return ResponseResult.okResult(mstinfoById);
    }



}

